﻿using KhoaLuan.Areas.Admin.ActionFilters;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KhoaLuan.Areas.Admin.Controllers
{
    [Security]
    public class OrderController : Controller
    {
        //
        // GET: /Admin/Order/
        LuanVanEntities db = new LuanVanEntities();
        public ActionResult Index()
        {
            var model = new Order();
            ViewBag.Orders = db.Orders
                .OrderByDescending(o=>o.OrderDate);
            return View(model);
        }
        public ActionResult Insert(Order model)
        {
            try
            {
                db.Orders.Add(model);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                ViewBag.Orders = db.Orders;
                return View("Index", model);
            }
        }
        public ActionResult Update(Order model)
        {
            try
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                ModelState.AddModelError("", "Cập nhật Thành Công");

            }
            catch(Exception ex)
            {
                ModelState.AddModelError("", ex.Message);

            }
            ViewBag.Orders = db.Orders;
            return View("Index", model);
        }
        public ActionResult Delete (int Id)
        {
            var model = db.Orders.Find(Id);
            db.Orders.Remove(model);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult Edit (int Id)
        {
            var model = db.Orders.Find(Id);
            ViewBag.Orders = db.Orders;
            return View("Index", model);
        }
	}
}